package com.example.rdcproject.ving.notes.service;

import java.util.concurrent.*;

/**
 * corePoolSize 核心线程数，也可以理解无论什么时候的线程池线程数都不会少于该数
 * maximumPoolSize 最大线程数，就是线程池线程数的最大值
 * keepAliveTime 线程被销毁空闲时间
 * unit     keepAliveTime的时间单位
 * workQueue   线程最大化是的多余任务存放的队列
 * threadFactory   线程工厂
 * handler  当队列满时，新任务的拒绝策略
 */
public class ThreadPoolService {

   int corePoolSize = 3;
   int maximumPoolSize = 5;
   int keepAliveTime = 10;
   TimeUnit unit = TimeUnit.SECONDS;
   BlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(10);
   ThreadFactory threadFactory = Executors.defaultThreadFactory();
   RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

   ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
           maximumPoolSize,
           keepAliveTime,
           unit,
           workQueue,
           threadFactory,
           handler);

   //提交任务给线程池
   public void execute(Runnable command) {
     threadPoolExecutor.execute(command);
   }

   //关闭线程池
   public void shutdown() {
     threadPoolExecutor.shutdown();
   }

}
